#include "volumetool.h"
#include "ui_volumetool.h"

VolumeTool::VolumeTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VolumeTool),
    isMuted(false),
    volumeRatio(20)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Popup|Qt::FramelessWindowHint|Qt::NoDropShadowWindowHint);
    //该函数的作用根据传入的参数，从左到右依次为：1.弹出式窗口 2.取消窗口的标题栏和边框 3.禁用窗口阴影

    ui->sliderBox->installEventFilter(this);


    setAttribute(Qt::WA_TranslucentBackground);//取消volumeTool窗口

    //设置volumeTool内部的窗口阴影
    QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setOffset(0,0);
    shadowEffect->setColor("#646464");
    shadowEffect->setBlurRadius(10);

    //给按钮设置图标
    ui->silenceBtn->setIcon(QIcon(":/images/volumn.png"));
    //设置，窗口的初始性质
    ui->outSlider->setGeometry(ui->outSlider->x(),180-36+25,ui->outSlider->width(),36);   //设置初始的音量
    ui->sliderBtn->move(ui->sliderBtn->x(),ui->outSlider->y()-ui->sliderBtn->height()/2);
    ui->volumeRatio->setText("20%");

    connect(ui->silenceBtn,&QPushButton::clicked,this,&VolumeTool::onSilenceBtnClicked);


}

VolumeTool::~VolumeTool()
{
    delete ui;
}

bool VolumeTool::eventFilter(QObject *object, QEvent *event)
{
    if(object == ui->sliderBox)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
            //如果是鼠标按下事件，修改sliderBtn和outLine的位置，并计算volumeRation
            setVolume();
            //emit setMusicVolume(volumeRatio);
        }
        else if(event->type() == QEvent::MouseButtonRelease)
        {
            //鼠标抬起，直接释放
            emit setMusicVolume(volumeRatio);
        }
        else if(event->type() == QEvent::MouseMove)
        {
            //鼠标移动事件
            setVolume();
            emit setMusicVolume(volumeRatio);
        }
        return true;
    }
    return QObject::eventFilter(object, event);
}

void VolumeTool::setVolume()
{
    int height = ui->sliderBox->mapFromGlobal(QCursor().pos()).y();//获取鼠标再sliderBox的相对坐标

    //将height限定在合法范围
    height = height<25?25:height;
    height = height>205?205:height;

    //重新计算ouline的长度,x坐标不用改变
    ui->outSlider->setGeometry(ui->outSlider->x(),height,ui->outSlider->width(),ui->sliderBox->height()-height-20);

    //重新计算sliderBtn位置
    ui->sliderBtn->move(ui->sliderBtn->x(),ui->outSlider->y());

    //计算音量
    volumeRatio = (int)(ui->outSlider->height()/(float)180*100);

    //更新下方的音量显示
    ui->volumeRatio->setText(QString::number(volumeRatio)+"%");
}

void VolumeTool::onSilenceBtnClicked()
{
    isMuted = !isMuted;

    if(!isMuted)
    {
        ui->silenceBtn->setIcon(QIcon(":images/volumn.png"));
    }
    else
    {
        ui->silenceBtn->setIcon(QIcon(":images/silent.png"));
    }
    emit setSilence(isMuted);
}

//void VolumeTool::paintEvent(QPaintEvent *event)
//{
//    qDebug()<<111<<endl;
//    (void)event;
//    //1.创建绘图对象
//    QPainter painter(this);
//    //2.设置抗锯齿
//    painter.setRenderHint(QPainter::Antialiasing,true);
//    //3.设置画笔
//    painter.setPen(Qt::NoPen);
//    //4.设置画刷颜色
//    painter.setBrush(Qt::white);
//    //创建一个三角形
//    QPolygon polygon;
//    polygon.append(QPoint(30,300));
//    polygon.append(QPoint(70,300));
//    polygon.append(QPoint(50,320));

//    //绘制
//    painter.drawPolygon(polygon);
//}
